package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.framework.redis.RedisCache;
import com.qinggeng.project.system.domain.Goods;
import com.qinggeng.project.system.domain.GoodsKeep;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.mapper.GoodsKeepMapper;
import com.qinggeng.project.system.mapper.GoodsMapper;
import com.qinggeng.project.system.mapper.OrdersGoodsMapper;
import com.qinggeng.project.system.service.IGoodsKeepService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品库存信息Service业务层处理
 *
 * @author qinggeng
 * @date 2020-06-15
 */
@Slf4j
@Service
public class GoodsKeepServiceImpl implements IGoodsKeepService {
    @Resource
    private GoodsKeepMapper goodsKeepMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private RedisCache redisCache;


    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private OrdersGoodsMapper ordersGoodsMapper;

    private final static String KEEP_OF_GOODS="keepOfGoods:";

    /**
     * 查询商品库存信息
     *
     * @param keepId 商品库存信息ID
     * @return 商品库存信息
     */
    @Override
    public GoodsKeep selectGoodsKeepById(Long keepId) {
        return goodsKeepMapper.selectGoodsKeepById(keepId);
    }

    /**
     * 查询商品库存信息列表
     *
     * @param goodsKeep 商品库存信息
     * @return 商品库存信息
     */
    @Override
    public List<GoodsKeep> selectGoodsKeepList(GoodsKeep goodsKeep) {
        return goodsKeepMapper.selectGoodsKeepList(goodsKeep);
    }

    /**
     * 新增商品库存信息
     *
     * @param goodsKeep 商品库存信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertGoodsKeep(GoodsKeep goodsKeep) {
        //根据商品编号查询该商品的标签
            //判断改商品有没有添加过库存
        GoodsKeep goodsKeep1 = goodsKeepMapper.selectGoodsKeepByGoodsId(goodsKeep.getGoodsId(), goodsKeep.getShopId());
        if (goodsKeep1 != null) {
            throw new CustomException("商品库存已存在", 500);
        }
        Goods goods = goodsMapper.selectGoodsById(goodsKeep.getGoodsId());
        if (goods.getGoodsMark() != null) {
            goodsKeep.setGoodsMark(goods.getGoodsMark());
        }
        goodsKeep.setCreateTime(DateUtils.getNowDate());
        int i = goodsKeepMapper.insertGoodsKeep(goodsKeep);
        if (i!=1){
            throw new CustomException("库存添加失败");
        }
//        redisCache.setCacheObject(KEEP_OF_GOODS+goodsKeep.getGoodsId(),goodsKeep.getKeepNum());
        return i;
    }

    /**
     * 修改商品库存信息
     *
     * @param goodsKeep 商品库存信息
     * @return 结果
     */
    @Override
    public int updateGoodsKeep(GoodsKeep goodsKeep) {
        goodsKeep.setUpdateTime(DateUtils.getNowDate());
        int i = goodsKeepMapper.updateGoodsKeep(goodsKeep);
        if (i != 1){
            throw new CustomException("库存修改失败");
        }
//        redisCache.setCacheObject(KEEP_OF_GOODS+goodsKeep.getGoodsId(),goodsKeep.getKeepNum());
        return i;
    }

    /**
     * 批量删除商品库存信息
     *
     * @param keepIds 需要删除的商品库存信息ID
     * @return 结果
     */
    @Override
    public int deleteGoodsKeepByIds(Long[] keepIds) {
        return goodsKeepMapper.deleteGoodsKeepByIds(keepIds);
    }

    /**
     * 删除商品库存信息信息
     *
     * @param keepId 商品库存信息ID
     * @return 结果
     */
    @Override
    public int deleteGoodsKeepById(Long keepId) {
        return goodsKeepMapper.deleteGoodsKeepById(keepId);
    }

    @Override
    public GoodsKeep selectGoodsKeepByGoodsId(Long goodsId,Long shopId) {
        return goodsKeepMapper.selectGoodsKeepByGoodsId(goodsId,shopId);
    }


    @Override
    public int subKeepNum(Long goodsId, Long shopId, Integer goodsNum) {
        return goodsKeepMapper.subKeepNum(goodsId,shopId,goodsNum);
    }

    @Override
    public int subKeepNum(Long goodsId, Long shopId, Integer keepNum, Integer goodsNum) {
        return goodsKeepMapper.subKeepNum1(goodsId,shopId,keepNum,goodsNum);
    }

    @Override
    public int addKeepNum(Long goodsId, Long shopId, Integer goodsNum) {

        return goodsKeepMapper.addKeepNum(goodsId,shopId,goodsNum);
    }

    public int addKeepNum(Orders orders){
        int rows=0;
        List<OrdersGoods> ordersGoodsList = ordersGoodsMapper.selectOrderGoodListByOrdersId(orders.getOrdersId());
        for (OrdersGoods ordersGoods : ordersGoodsList) {
            try {
//                redisTemplate.opsForValue().increment(KEEP_OF_GOODS + ordersGoods.getGoodsId(), ordersGoods.getGoodsNum());
                int result = this.addKeepNum(ordersGoods.getGoodsId(), orders.getShopId(), ordersGoods.getGoodsNum());
                if (result > 1) {
                    rows++;
                }
            } catch (Exception e) {
                log.error("库存恢复失败：" + e.getMessage());
            }
        }
        return rows;
    }

    /**
     * 销量增加
     * @param number
     */
    @Override
    public void updateGoodsKeepAutoAddSale(Integer number) {
        goodsKeepMapper.updateGoodsKeepAutoAddSale(number);
    }



}
